package flappy.bird.cover.resource;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class AppResource {
	public static Image splitted[];
	public static Image img1, bg;
	public static Image background, background_bottom;
	public static Image about, help, exit, play, logo;

	public AppResource() {
	}

	public static Image CreateScaledImage(Image imgOldImage, int iNewWidth,
			int iNewHeight) {

		Image imgNewImage = null;
		final int iOldWidth = imgOldImage.getWidth();
		final int iOldHeight = imgOldImage.getHeight();
		int iOldRGBArray[] = new int[iOldWidth * iOldHeight];
		imgOldImage.getRGB(iOldRGBArray, 0, iOldWidth, 0, 0, iOldWidth,
				iOldHeight);
		int iNewRGBArray[] = new int[iNewWidth * iNewHeight];

		for (int yy = 0; yy < iNewHeight; yy++) {
			int dy = yy * iOldHeight / iNewHeight;

			for (int xx = 0; xx < iNewWidth; xx++) {
				int dx = xx * iOldWidth / iNewWidth;

				iNewRGBArray[(iNewWidth * yy) + xx] = iOldRGBArray[(iOldWidth * dy)
						+ dx];
			}
		}

		imgNewImage = Image.createRGBImage(iNewRGBArray, iNewWidth, iNewHeight,
				true);

		return imgNewImage;

	}

	public static Image zoomImage(Image img) {

		Image outImg = null;

		int[] rgbOutput = null;
		Graphics g = null;
		int width = 0, height = 0;

		try {

			width = img.getWidth();
			height = img.getHeight();

			outImg = Image.createImage(width << 1, height << 1);

			int rgbInput[] = new int[width * height];
			rgbOutput = new int[(width << 1) * (height << 1)];

			img.getRGB(rgbInput, 0, width, 0, 0, width, height);

			int i, j, k;
			k = 0;
			for (i = 0; i < (height << 1); i += 2) {
				for (j = 0; j < (width << 1); j += 2) {
					rgbOutput[i * (width << 1) + j] = rgbInput[k];
					rgbOutput[(i + 1) * (width << 1) + j] = rgbInput[k];
					rgbOutput[i * (width << 1) + j + 1] = rgbInput[k];
					rgbOutput[(i + 1) * (width << 1) + j + 1] = rgbInput[k];
					k++;
				}
			}

			g = outImg.getGraphics();
		} catch (Exception e) {
		}

		return outImg.createRGBImage(rgbOutput, width << 1, height << 1, true);
	}

	public void loadLevel() {
		// tinh toa do vi tri

		try {
			background_bottom = Image.createImage("/bg_bottom.png");
			background = Image.createImage("/bg.png");
			play = Image.createImage("/play.png");
			help = Image.createImage("/help.png");
			about = Image.createImage("/about.png");
			exit = Image.createImage("/exit.png");
			logo = Image.createImage("/logo.png");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// ////////////////////////////
		splitted = new Image[4];
		try {
			Image tmp;
			img1 = Image.createImage("/bird.png");

			tmp = Image.createImage(img1, 0, 0, 32, 23, 0);
			splitted[0] = AppResource.CreateScaledImage(tmp, 28, 20);

			tmp = Image.createImage(img1, 32, 0, 32, 23, 0);
			splitted[1] = AppResource.CreateScaledImage(tmp, 28, 20);

			tmp = Image.createImage(img1, 64, 0, 32, 23, 0);
			splitted[2] = AppResource.CreateScaledImage(tmp, 28, 20);
			
			tmp = Image.createImage(img1, 64, 0, 32, 23,Sprite.TRANS_ROT90 );
			splitted[3] = AppResource.CreateScaledImage(tmp, 20, 28);

		} catch (Exception e) {
		}
	}

	public void loadResource() {
	}
}